package cn.kgc.boot.vue.controller;

import cn.kgc.boot.vue.entity.Student;
import cn.kgc.boot.vue.response.ResponseResult;
import cn.kgc.boot.vue.service.StudentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 课工场
 * @date 2025/9/12
 * @description
 */
@RestController
@RequestMapping("student")
@CrossOrigin
public class StudentController {

    @Resource
    private StudentService studentService;

    // 学生信息分页   /student/page
    @RequestMapping("page")
    public ResponseResult studentPage(Integer page, Integer size) {

        Page<Student> studentPage = new Page<>(page, size);
        studentService.page(studentPage);

        long total = studentPage.getTotal();
        List<Student> records = studentPage.getRecords();

        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(records);
        responseResult.setCode(200);
        responseResult.setCount(total);
        return responseResult;
    }


    // 学生信息批量删除
    @RequestMapping("delete")
    public ResponseResult deleteStudents(@RequestBody List<Integer> ids) {
        // 批量删除
        ResponseResult responseResult = new ResponseResult();
        try {
            studentService.removeByIds(ids);
            responseResult.setCode(200);
            responseResult.setMessage("删除成功");

        }catch (Exception e){
            e.printStackTrace();
            responseResult.setCode(500);
            responseResult.setMessage("删除失败");
        }
        return  responseResult;
    }


}
